/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hiruss.imageprocessor.ui;

import com.hiruss.imageprocessor.util.Histogram;
import com.hiruss.imageprocessor.util.HoughTransform;
import com.hiruss.imageprocessor.util.ImageHandler;
import com.hiruss.imageprocessor.util.Segmentation;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author Hiru
 */
public class SegmentationPanel extends javax.swing.JDialog {

    private UI parent;
    private static SegmentationPanel instance;

    /**
     * Creates new form CropPanel
     */
    private SegmentationPanel(UI parent, boolean modal) {
        super(parent, modal);
        this.parent = parent;
        initComponents();
        setLocationRelativeTo(parent);
    }

    public static SegmentationPanel getInstance(UI parent) {
        if (SegmentationPanel.instance == null) {
            SegmentationPanel.instance = new SegmentationPanel(parent, true);
        }
        instance.revertButton.setEnabled(false);
        return SegmentationPanel.instance;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        resamplingMethodButtonGroup = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        applyButton = new javax.swing.JButton();
        revertButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        panel = new javax.swing.JPanel();
        contourTrackingTTextField = new javax.swing.JTextField();
        histogramSmoothingWidthTextField = new javax.swing.JTextField();
        houghTransformRadioButton = new javax.swing.JRadioButton();
        intermeansRadioButton = new javax.swing.JRadioButton();
        contourTrackingRadioButton = new javax.swing.JRadioButton();
        histogramRadioButton = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        initialTTextField = new javax.swing.JTextField();
        iterationsTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        solutionLabel = new javax.swing.JLabel();
        histogramPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Segmentation");
        setBounds(new java.awt.Rectangle(0, 0, 400, 120));
        setMinimumSize(new java.awt.Dimension(620, 180));
        setModal(true);
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanel1.setLayout(new java.awt.GridLayout(1, 3, 6, 0));

        applyButton.setText("Apply");
        applyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                applyButtonActionPerformed(evt);
            }
        });
        jPanel1.add(applyButton);

        revertButton.setText("Revert");
        revertButton.setEnabled(false);
        revertButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                revertButtonActionPerformed(evt);
            }
        });
        jPanel1.add(revertButton);

        closeButton.setText("Close");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });
        jPanel1.add(closeButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(6, 12, 12, 12);
        getContentPane().add(jPanel1, gridBagConstraints);

        panel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        panel.setLayout(new java.awt.GridBagLayout());

        contourTrackingTTextField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
        contourTrackingTTextField.setText("50");
        contourTrackingTTextField.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 6);
        panel.add(contourTrackingTTextField, gridBagConstraints);

        histogramSmoothingWidthTextField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
        histogramSmoothingWidthTextField.setText("2");
        histogramSmoothingWidthTextField.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 6);
        panel.add(histogramSmoothingWidthTextField, gridBagConstraints);

        resamplingMethodButtonGroup.add(houghTransformRadioButton);
        houghTransformRadioButton.setText("Hough Transform (Line Detection)");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 6, 3, 3);
        panel.add(houghTransformRadioButton, gridBagConstraints);

        resamplingMethodButtonGroup.add(intermeansRadioButton);
        intermeansRadioButton.setText("Intermeans Algorithm");
        intermeansRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                intermeansRadioButtonStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 6, 3, 3);
        panel.add(intermeansRadioButton, gridBagConstraints);

        resamplingMethodButtonGroup.add(contourTrackingRadioButton);
        contourTrackingRadioButton.setText("Contour Tracking T =");
        contourTrackingRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                contourTrackingRadioButtonStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        panel.add(contourTrackingRadioButton, gridBagConstraints);

        resamplingMethodButtonGroup.add(histogramRadioButton);
        histogramRadioButton.setText("Histogram Smoothing Width =");
        histogramRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                histogramRadioButtonStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        panel.add(histogramRadioButton, gridBagConstraints);

        jLabel1.setForeground(new java.awt.Color(255, 0, 51));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Any edge detection algorithm has to be applied first");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(6, 0, 6, 0);
        panel.add(jLabel1, gridBagConstraints);

        initialTTextField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
        initialTTextField.setText("50");
        initialTTextField.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 6, 6);
        panel.add(initialTTextField, gridBagConstraints);

        iterationsTextField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
        iterationsTextField.setText("2");
        iterationsTextField.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 6, 6);
        panel.add(iterationsTextField, gridBagConstraints);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel2.setText("Initial T =");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(3, 6, 6, 3);
        panel.add(jLabel2, gridBagConstraints);

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel3.setText("Iterations");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 6, 3);
        panel.add(jLabel3, gridBagConstraints);

        solutionLabel.setText("Solution:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(3, 12, 6, 3);
        panel.add(solutionLabel, gridBagConstraints);

        histogramPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        histogramPanel.setLayout(new java.awt.GridLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        panel.add(histogramPanel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.2;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 6, 12);
        getContentPane().add(panel, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void applyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_applyButtonActionPerformed

        setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
        applyButton.setEnabled(false);
        try {
            BufferedImage originalImage = parent.getEditingImage();
            BufferedImage destImage = ImageHandler.duplicateImage(originalImage);

            if (houghTransformRadioButton.isSelected()) {
                HoughTransform.applyHoughTransform(destImage);
            } else if (intermeansRadioButton.isSelected()) {
                int threshold = Integer.valueOf(initialTTextField.getText());
                int iterations = Integer.valueOf(iterationsTextField.getText());
                Histogram histo = new Histogram();
                histo.load(destImage);
                int newThreshold = histo.doIntermeans(threshold, iterations);
                solutionLabel.setText("Solution: " + String.valueOf(newThreshold));
            } else if (contourTrackingRadioButton.isSelected()) {
                int threashold = Integer.parseInt(contourTrackingTTextField.getText());
                Segmentation.THRESHOLD = threashold;
                destImage = Segmentation.segmentContour(originalImage);
            } else if (histogramRadioButton.isSelected()) {
                int width = Integer.parseInt(histogramSmoothingWidthTextField.getText());
                histogramPanel.removeAll();
                histogramPanel.setLayout(new GridBagLayout());
                histogramPanel.add(new JLabel("Normal Histogram", JLabel.CENTER), new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(12, 12, 3, 6), 0, 24));
                histogramPanel.add(new JLabel("Smoothed Histogram", JLabel.CENTER), new GridBagConstraints(1, 0, 1, 1, 0.1, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(12, 6, 3, 12), 0, 24));

                Histogram histogram = Histogram.getHistogram(destImage);
                histogramPanel.add(histogram, new GridBagConstraints(0, 1, 1, 1, 0.1, 0.1,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(3, 12, 12, 6), 0, 0));

                Histogram histogramSmoothed = Histogram.getHistogramSmoothen(destImage, width);
                histogramPanel.add(histogramSmoothed, new GridBagConstraints(1, 1, 1, 1, 0.1, 0.1,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(3, 6, 12, 12), 0, 0));

                this.setSize(this.getWidth(), 420);
                histogramPanel.repaint();
            }
            parent.setEditingImage(destImage);
            revertButton.setEnabled(true);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this,
                    e.getLocalizedMessage(),
                    "Invalid Inputs!",
                    JOptionPane.WARNING_MESSAGE);
        }
        applyButton.setEnabled(true);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_applyButtonActionPerformed

    private void revertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_revertButtonActionPerformed

        parent.revert();
        revertButton.setEnabled(false);
    }//GEN-LAST:event_revertButtonActionPerformed

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed

        dispose();
    }//GEN-LAST:event_closeButtonActionPerformed

    private void contourTrackingRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_contourTrackingRadioButtonStateChanged

        contourTrackingTTextField.setEnabled(contourTrackingRadioButton.isSelected());
    }//GEN-LAST:event_contourTrackingRadioButtonStateChanged

    private void histogramRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_histogramRadioButtonStateChanged

        histogramSmoothingWidthTextField.setEnabled(histogramRadioButton.isSelected());
    }//GEN-LAST:event_histogramRadioButtonStateChanged

    private void intermeansRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_intermeansRadioButtonStateChanged

        initialTTextField.setEnabled(intermeansRadioButton.isSelected());
        iterationsTextField.setEnabled(intermeansRadioButton.isSelected());
    }//GEN-LAST:event_intermeansRadioButtonStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton applyButton;
    private javax.swing.JButton closeButton;
    private javax.swing.JRadioButton contourTrackingRadioButton;
    private javax.swing.JTextField contourTrackingTTextField;
    private javax.swing.JPanel histogramPanel;
    private javax.swing.JRadioButton histogramRadioButton;
    private javax.swing.JTextField histogramSmoothingWidthTextField;
    private javax.swing.JRadioButton houghTransformRadioButton;
    private javax.swing.JTextField initialTTextField;
    private javax.swing.JRadioButton intermeansRadioButton;
    private javax.swing.JTextField iterationsTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel panel;
    private javax.swing.ButtonGroup resamplingMethodButtonGroup;
    private javax.swing.JButton revertButton;
    private javax.swing.JLabel solutionLabel;
    // End of variables declaration//GEN-END:variables
}
